<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css" integrity="sha256-AbA177XfpSnFEvgpYu1jMygiLabzPCJCRIBtR5jGc0k=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"fengxun2017.github.io","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.13.2","exturl":false,"sidebar":{"position":"right","display":"hide","padding":18,"offset":12},"copycode":{"enable":false,"style":null},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}}</script><script src="/js/config.js"></script>

    <meta name="description" content="前提：首先需要基于自己的开发板实现一个点灯的程序，确保工程编译通过，烧录后程序能正常运行。">
<meta property="og:type" content="article">
<meta property="og:title" content="基于crotex-m处理器新建FreeRTOS工程">
<meta property="og:url" content="http://fengxun2017.github.io/2022/10/25/%E5%9F%BA%E4%BA%8Ecrotex-m%E5%A4%84%E7%90%86%E5%99%A8%E6%96%B0%E5%BB%BAFreeRTOS%E5%B7%A5%E7%A8%8B/index.html">
<meta property="og:site_name" content="fx">
<meta property="og:description" content="前提：首先需要基于自己的开发板实现一个点灯的程序，确保工程编译通过，烧录后程序能正常运行。">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-10-24T16:00:00.000Z">
<meta property="article:modified_time" content="2022-11-15T14:57:39.926Z">
<meta property="article:author" content="feng xun">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://fengxun2017.github.io/2022/10/25/%E5%9F%BA%E4%BA%8Ecrotex-m%E5%A4%84%E7%90%86%E5%99%A8%E6%96%B0%E5%BB%BAFreeRTOS%E5%B7%A5%E7%A8%8B/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://fengxun2017.github.io/2022/10/25/%E5%9F%BA%E4%BA%8Ecrotex-m%E5%A4%84%E7%90%86%E5%99%A8%E6%96%B0%E5%BB%BAFreeRTOS%E5%B7%A5%E7%A8%8B/","path":"2022/10/25/基于crotex-m处理器新建FreeRTOS工程/","title":"基于crotex-m处理器新建FreeRTOS工程"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>基于crotex-m处理器新建FreeRTOS工程 | fx</title>
  






  <script async defer data-website-id="" src=""></script>

  <script defer data-domain="" src=""></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
  <a target="_blank" rel="noopener" href="https://github.com/fengxun2017" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#70B7FD; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">fx</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">FreeRTOS交流QQ群-663806972 BLE学习交流群-498676838</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%8F%90%E5%8F%96FreeRtos%E5%BF%85%E8%A6%81%E6%96%87%E4%BB%B6"><span class="nav-number">1.</span> <span class="nav-text">提取FreeRtos必要文件</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#FreeRtos%E5%8A%9F%E8%83%BD%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6FreeRTOSConfig-h"><span class="nav-number">2.</span> <span class="nav-text">FreeRtos功能配置文件FreeRTOSConfig.h</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%BE%93%E5%87%BA%E8%B0%83%E8%AF%95%E4%BF%A1%E6%81%AF"><span class="nav-number">3.</span> <span class="nav-text">输出调试信息</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%AE%9E%E7%8E%B0%E4%B8%80%E4%B8%AA%E7%BF%BB%E8%BD%ACLED%E7%9A%84%E6%B5%8B%E8%AF%95%E7%A8%8B%E5%BA%8F"><span class="nav-number">4.</span> <span class="nav-text">实现一个翻转LED的测试程序</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">feng xun</p>
  <div class="site-description" itemprop="description">FreeRTOS交流QQ群-663806972 BLE学习交流群-498676838</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">96</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">16</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
  </nav>
</div>
  <div class="cc-license site-overview-item animated" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" class="cc-opacity" rel="noopener" target="_blank"><img src="https://cdnjs.cloudflare.com/ajax/libs/creativecommons-vocabulary/2020.11.3/assets/license_badges/small/by_nc_sa.svg" alt="Creative Commons"></a>
  </div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://fengxun2017.github.io/2022/10/25/%E5%9F%BA%E4%BA%8Ecrotex-m%E5%A4%84%E7%90%86%E5%99%A8%E6%96%B0%E5%BB%BAFreeRTOS%E5%B7%A5%E7%A8%8B/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="feng xun">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="fx">
      <meta itemprop="description" content="FreeRTOS交流QQ群-663806972 BLE学习交流群-498676838">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="基于crotex-m处理器新建FreeRTOS工程 | fx">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          基于crotex-m处理器新建FreeRTOS工程
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-10-25 00:00:00" itemprop="dateCreated datePublished" datetime="2022-10-25T00:00:00+08:00">2022-10-25</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/FreeRTOS/" itemprop="url" rel="index"><span itemprop="name">FreeRTOS</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>7.1k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>9 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <center>

<p><strong>前提</strong>：首先需要基于自己的开发板实现一个点灯的程序，确保工程编译通过，烧录后程序能正常运行。</p>
</center>

<span id="more"></span>

<hr>
<h4 id="提取FreeRtos必要文件"><a href="#提取FreeRtos必要文件" class="headerlink" title="提取FreeRtos必要文件"></a>提取FreeRtos必要文件</h4><ol>
<li>FreeRtos<a target="_blank" rel="noopener" href="https://freertos.org/a00104.html">官网</a>下载源码</li>
<li>自己工程里新建一个目录用来保存提取的FreeRtos核心必要文件</li>
<li>提取如下文件保存到自己新建的文件夹中<ul>
<li><p>3.1 FreeRTOS&#x2F;Source&#x2F;tasks.c <font color="#008000">&#x2F;&#x2F; 包含FreeRtos核心功能和数据，如任务的创建、调度。</font></p>
</li>
<li><p>3.2 FreeRTOS&#x2F;Source&#x2F;list.c <font color="#008000">&#x2F;&#x2F; tasks.c中的任务就绪任务列表、阻塞任务列表都是基于 list.c中的连接数据结构来实现的。</font></p>
</li>
<li><p>3.3 FreeRTOS&#x2F;Source&#x2F;queue.c <font color="#008000">&#x2F;&#x2F; 提供基础的队列应用功能，同时freertos的信号量、互斥量也是基于队列来实现的。</font></p>
</li>
<li><p>3.4 FreeRTOS&#x2F;Source&#x2F;portable&#x2F;[compiler]&#x2F;[architecture]&#x2F;port.c. <font color="#008000">&#x2F;&#x2F; 与实际MCU和使用的编译工具相关的文件，例如使用keil工具和crotex-m4的mcu时可以选择：&#x2F;RVDS&#x2F;ARM_CM4F下的文件(port.c portmacro.h)</font><br> <font color="#008000">如果是较新的Keil工具，使用了V6版本的编译工具，由于和旧版本的汇编不兼容，需要替换成&#x2F;GCC&#x2F;ARM_CM4下的文件。(实际应该使用ARMClang目录下的，但这个目录下的文件指明应该使用&#x2F;GCC目录下的，即新版本编译器是兼容GCC语法的)</font></p>
</li>
<li><p>3.5 FreeRTOS&#x2F;Source&#x2F;portable&#x2F;MemMang&#x2F;heap_3.c <font color="#008000">&#x2F;&#x2F; FreeRtos内核默认创建系统相关数据结构时会使用动态内存分配，因此需要提供一个动态内存分配的算法，这里直接使用heap_3.c，即内容就是对 c标准库 malloc、free的封装。熟悉后可以根据自己的应用场景选择FreeRtos提供的其它合适方案</font></p>
</li>
<li><p>3.6 拷贝文件夹FreeRTOS&#x2F;Source&#x2F;include 到自己的工程中，并将其添加到工程的头文件目录配置中。</p>
</li>
<li><p>前面 3.4 拷贝的 portmacro.h 文件所在目录也要添加到工程头文件目录配置中。</p>
</li>
</ul>
</li>
</ol>
<br>

<h4 id="FreeRtos功能配置文件FreeRTOSConfig-h"><a href="#FreeRtos功能配置文件FreeRTOSConfig-h" class="headerlink" title="FreeRtos功能配置文件FreeRTOSConfig.h"></a>FreeRtos功能配置文件FreeRTOSConfig.h</h4><p>移植完上面的文件，就搭建好了FreeRtos的源码环境。但是想要实际运行起来，还需要一个应用配置文件，配置一些全局参数。FreeRtos的源码中有很多Demo都已配置好FreeRTOSConfig.h文件，一般找一个同系列的芯片配置，进行小改动下就能用了。</p>
<p>例如，FreeRtos的任务调度周期需要设置多少，即多久产生一时钟中断，该中断即系统的时钟滴答(tick)，是rtos能运行的核心。例如，当调用vTaskDelay 函数时，当前任务即进入阻塞态，那么rtos如何知道何时唤醒这个被阻塞的任务？就是基于tick中断来统计当前运行了多久，以及是否需要唤醒某个被阻塞的任务。</p>
<p>如下，将内核设置为 1秒中产生 10 次时钟滴答，即调度周期为100ms：<br><code>#define configTICK_RATE_HZ			( ( TickType_t ) 10 )</code><br>内核的时钟滴答越快，精度越高(例如FreeRtos提供的vTaskDelay的精度)，但由于频繁产生中断并执行内核调度过程，功耗也会相应增加，所以需要更具实际应用选择一个合适的tick频率。</p>
<p>那么设置了 <code>configTICK_RATE_HZ=10</code> 后如何就能让系统没 100ms 产生一次中断？<br>crotex-m系列的内核都自带SysTick定时器，FreeRtos默认使用该定时器来产生定时中断来作为内核的tick，FreeRtos默认配置使用内核时钟作为时钟源的计数器，所以知道了MCU的运行频率，FreeRtos就可以计算出，应该给SysTick计数器配置成多少值，才能让其每100ms产生一次中断。例如，我的MCU运行在 64M:<br><code>#define configCPU_CLOCK_HZ			( ( unsigned long ) 64000000 )	</code><br>具体如何根据<code>configTICK_RATE_HZ</code>和<code>configCPU_CLOCK_HZ</code>来配置SysTick，可以在前面移植的文件port.c中的vPortSetupTimerInterrup 函数中查看。</p>
<p>另一个需要配置的重要参数：<br><code>#define configPRIO_BITS       		3 </code><br>该值定义了几个bit来进行优先级分组配置（cortex-m3&#x2F;4内核定义的优先级寄存器是具有8 bit的，但是芯片厂商是可以自己决定使用更少的bit来简化芯片和降低功耗，一般芯片厂商只会用3 bit或4 bit）。所以这个值需要根据你使用的MCU来决定(芯片手册里肯定能找到相关描述)。</p>
<p>此外，FreeRTOS默认配置是抢占式调度系统，即优先级高的任务会立即抢占优先级低的任务，这里的优先级指的是FreeRTOS自己定义的优先级，FreeRTOS一共可以有的优先级是通过<code>configMAX_PRIORITIES</code>来配置的，即创建任务时允许的优先级范围是[0,configMAX_PRIORITIES-1]，数值越大，优先级越高，越优先被调度。如果有多个任务的优先级相同，且处于当前最高优先级，FreeRTOS默认是会轮流调度它们(FreeRTOS的相同优先级时间片调度配置项<code>configUSE_TIME_SLICING</code>默认配置就是1)。</p>
<p>我们的样例需要闪烁LED等，需要使用FreeRtos的延迟函数，所以还需要添加如下定义:<br><code>#define INCLUDE_vTaskDelay				1</code></p>
<p>其余值使用默认即可，例如一个样例配置文件FreeRTOSConfig.h：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;SEGGER_RTT.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> configUSE_PREEMPTION		1</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> configUSE_IDLE_HOOK			0</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> configUSE_TICK_HOOK			0</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> configCPU_CLOCK_HZ			( ( unsigned long ) 64000000 )	</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> configTICK_RATE_HZ			( ( TickType_t ) 10 )</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> configMAX_PRIORITIES		( 5 )</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> configMINIMAL_STACK_SIZE	( ( unsigned short ) 128 )</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> configTOTAL_HEAP_SIZE		( ( size_t ) ( 10 * 1024 ) )</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> configMAX_TASK_NAME_LEN		( 16 )</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> configUSE_TRACE_FACILITY	0</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> configUSE_16_BIT_TICKS		0</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> configIDLE_SHOULD_YIELD		1</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* Co-routine definitions. */</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> configUSE_CO_ROUTINES 		0</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> configMAX_CO_ROUTINE_PRIORITIES ( 2 )</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* Software timer definitions. */</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> configUSE_TIMERS				1</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> configTIMER_TASK_PRIORITY		( 2 )</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> configTIMER_QUEUE_LENGTH		10</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> configTIMER_TASK_STACK_DEPTH	( configMINIMAL_STACK_SIZE * 2 )</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* Set the following definitions to 1 to include the API function, or zero</span></span><br><span class="line"><span class="comment">to exclude the API function. */</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> INCLUDE_vTaskPrioritySet		1</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> INCLUDE_uxTaskPriorityGet		1</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> INCLUDE_vTaskDelete				1</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> INCLUDE_vTaskCleanUpResources	1</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> INCLUDE_vTaskSuspend			1</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> INCLUDE_vTaskDelayUntil			1</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> INCLUDE_vTaskDelay				1</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> INCLUDE_xTaskGetCurrentTaskHandle 1</span></span><br><span class="line"><span class="comment">/* Cortex-M specific definitions. */</span></span><br><span class="line"><span class="meta">#<span class="keyword">ifdef</span> __NVIC_PRIO_BITS</span></span><br><span class="line">	<span class="comment">/* __BVIC_PRIO_BITS will be specified when CMSIS is being used. */</span></span><br><span class="line">	<span class="meta">#<span class="keyword">define</span> configPRIO_BITS       		__NVIC_PRIO_BITS</span></span><br><span class="line"><span class="meta">#<span class="keyword">else</span></span></span><br><span class="line">	<span class="meta">#<span class="keyword">define</span> configPRIO_BITS       		3        <span class="comment">/* 15 priority levels */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* The lowest interrupt priority that can be used in a call to a &quot;set priority&quot;</span></span><br><span class="line"><span class="comment">function. */</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> configLIBRARY_LOWEST_INTERRUPT_PRIORITY			0xf</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* The highest interrupt priority that can be used by any interrupt service</span></span><br><span class="line"><span class="comment">routine that makes calls to interrupt safe FreeRTOS API functions.  DO NOT CALL</span></span><br><span class="line"><span class="comment">INTERRUPT SAFE FREERTOS API FUNCTIONS FROM ANY INTERRUPT THAT HAS A HIGHER</span></span><br><span class="line"><span class="comment">PRIORITY THAN THIS! (higher priorities are lower numeric values. */</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY	5</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* Interrupt priorities used by the kernel port layer itself.  These are generic</span></span><br><span class="line"><span class="comment">to all Cortex-M ports, and do not rely on any particular library functions. */</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> configKERNEL_INTERRUPT_PRIORITY 		( configLIBRARY_LOWEST_INTERRUPT_PRIORITY &lt;&lt; (8 - configPRIO_BITS) )</span></span><br><span class="line"><span class="comment">/* !!!! configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to zero !!!!</span></span><br><span class="line"><span class="comment">See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html. */</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> configMAX_SYSCALL_INTERRUPT_PRIORITY 	( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY &lt;&lt; (8 - configPRIO_BITS) )</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/* Definitions that map the FreeRTOS port interrupt handlers to their CMSIS</span></span><br><span class="line"><span class="comment">standard names. */</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> vPortSVCHandler SVC_Handler</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> xPortPendSVHandler PendSV_Handler</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> xPortSysTickHandler SysTick_Handler</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">extern</span> <span class="type">void</span> <span class="title function_">vAssertCalled</span><span class="params">( <span class="type">const</span> <span class="type">char</span> * pcFile, <span class="type">uint32_t</span> ulLine )</span>;</span><br><span class="line"><span class="comment">/* Normal assert() semantics without relying on the provision of an assert.h</span></span><br><span class="line"><span class="comment">header file. */</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> configASSERT( x ) <span class="keyword">if</span>( ( x ) == 0 )	vAssertCalled( __FILE__, __LINE__ )</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><strong>注意</strong>：对于cortex-m处理器，一般启动文件(工程中的那个startup.s汇编文件，一般都是芯片厂商提供的)中都是默认使用CMSIS标准的名称，如<code>SysTick_Handler</code>，而Port.c文件中函数定义的名字用的是<code>xPortSysTickHandler</code>，因此需要在配置文件FreeRTOSConfig.h中使用<code>#define</code> 将其改名:</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> vPortSVCHandler SVC_Handler</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> xPortPendSVHandler PendSV_Handler</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> xPortSysTickHandler SysTick_Handler</span></span><br></pre></td></tr></table></figure>
<br>

<h4 id="输出调试信息"><a href="#输出调试信息" class="headerlink" title="输出调试信息"></a>输出调试信息</h4><p>在前面的FreeRTOSConfig.h中我们重定义了断言函数，FreeRtos在运行时会有充分的配置和参数检查，在调试阶段打开这个断言函数，可以快速定位一些配置和参数不正确的问题。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">extern</span> <span class="type">void</span> <span class="title function_">vAssertCalled</span><span class="params">( <span class="type">const</span> <span class="type">char</span> * pcFile, <span class="type">uint32_t</span> ulLine )</span>;</span><br><span class="line"><span class="meta">#<span class="keyword">define</span> configASSERT( x ) <span class="keyword">if</span>( ( x ) == 0 )  vAssertCalled( __FILE__, __LINE__ )</span></span><br></pre></td></tr></table></figure>
<p><code>vAssertCalled( const char * pcFile, uint32_t ulLine )</code>的具体实现可以使用任意的工具(例如通过串口工具)将出现问题的函数所在的文件名和行号显示出来。这里，我使用了J-Link RTT工具来输出错误日志信息，J-Link RTT的使用可以参考这篇文章：<a href="https://fengxun2017.github.io/2022/10/20/use-jlink-RTT/">使用JLink-RTT输出调试信息</a></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">vAssertCalled</span><span class="params">( <span class="type">const</span> <span class="type">char</span> * pcFile, <span class="type">uint32_t</span> ulLine )</span>&#123;</span><br><span class="line">    SEGGER_RTT_printf(<span class="number">0</span>, <span class="string">&quot;error at:%s, line:%d\r\n&quot;</span>, pcFile, ulLine);</span><br><span class="line">    <span class="keyword">for</span>(;;)&#123;&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<br>

<h4 id="实现一个翻转LED的测试程序"><a href="#实现一个翻转LED的测试程序" class="headerlink" title="实现一个翻转LED的测试程序"></a>实现一个翻转LED的测试程序</h4><ol>
<li>首先确保自己的裸机LED翻转测试可以正常运行。 <br></li>
<li>创建每500ms翻转一个LED的任务：其中LED硬件相关代码使用自己的 <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">vTaskCode</span><span class="params">( <span class="type">void</span> * pvParameters )</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">const</span> TickType_t xDelay = <span class="number">500</span> / portTICK_PERIOD_MS;</span><br><span class="line">    bsp_board_leds_init();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">while</span>(<span class="number">1</span>) &#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">uint32_t</span> i = <span class="number">0</span>; i &lt; LEDS_NUMBER; i++)</span><br><span class="line">        &#123;</span><br><span class="line">            vTaskDelay( xDelay );</span><br><span class="line">            nrf_gpio_pin_toggle(m_board_led_list[i]);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
 <br></li>
<li>实现main函数：<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdint.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdbool.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;FreeRTOS.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;task.h&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">void</span>)</span> &#123;</span><br><span class="line"></span><br><span class="line">    BaseType_t xReturned;</span><br><span class="line">    TaskHandle_t xHandle = <span class="literal">NULL</span>;</span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line">    <span class="comment">/* Create the task, storing the handle. */</span></span><br><span class="line">    xReturned = xTaskCreate(</span><br><span class="line">                    vTaskCode,       <span class="comment">/* Function that implements the task. */</span></span><br><span class="line">                    <span class="string">&quot;NAME&quot;</span>,          <span class="comment">/* Text name for the task. */</span></span><br><span class="line">                    configMINIMAL_STACK_SIZE,      <span class="comment">/* Stack size in words, not bytes. */</span></span><br><span class="line">                    <span class="literal">NULL</span>,    <span class="comment">/* Parameter passed into the task. */</span></span><br><span class="line">                    <span class="number">2</span>,<span class="comment">/* Priority at which the task is created. */</span></span><br><span class="line">                    &amp;xHandle );      <span class="comment">/* Used to pass out the created task&#x27;s handle. */</span></span><br><span class="line">    </span><br><span class="line">    <span class="comment">/* Start the created tasks running. */</span></span><br><span class="line">    vTaskStartScheduler();</span><br><span class="line">    </span><br><span class="line">    <span class="comment">/* Execution will only reach here if there was insufficient heap to</span></span><br><span class="line"><span class="comment">    start the scheduler. */</span></span><br><span class="line">    <span class="keyword">for</span>( ;; );</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;    </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ol>
<p>烧录到开发板上，配置没有问题的话，这里应该就能看到自己开发板的LED闪烁了。</p>
<br>
FreeRTOS交流QQ群-663806972

<br>
<br>
<br>
<br>

<p>参考链接：<a target="_blank" rel="noopener" href="https://freertos.org/Creating-a-new-FreeRTOS-project.html">https://freertos.org/Creating-a-new-FreeRTOS-project.html</a></p>

    </div>

    
    
    

    <footer class="post-footer">
          

<div class="post-copyright">
<ul>
  <li class="post-copyright-author">
      <strong>本文作者： </strong>feng xun
  </li>
  <li class="post-copyright-link">
      <strong>本文链接：</strong>
      <a href="http://fengxun2017.github.io/2022/10/25/%E5%9F%BA%E4%BA%8Ecrotex-m%E5%A4%84%E7%90%86%E5%99%A8%E6%96%B0%E5%BB%BAFreeRTOS%E5%B7%A5%E7%A8%8B/" title="基于crotex-m处理器新建FreeRTOS工程">http://fengxun2017.github.io/2022/10/25/基于crotex-m处理器新建FreeRTOS工程/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2022/10/20/use-jlink-RTT/" rel="prev" title="使用JLink-RTT输出调试信息">
                  <i class="fa fa-chevron-left"></i> 使用JLink-RTT输出调试信息
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2022/10/27/why-use-RTOS/" rel="next" title="为什么使用嵌入式实时操作系统(RTOS)">
                  为什么使用嵌入式实时操作系统(RTOS) <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2025</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">feng xun</span>
</div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">552k</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">11:30</span>
  </span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  





  





</body>
</html>
